using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Configuration;
using System.Collections.Specialized;

namespace Chimera2.Classes
{
    public class Datahandler
    {
        public Settings LoadSettings()
        {
            Settings set = new Settings();

            string xml = "";
            
            if (File.Exists("Hubs.xml"))
            {
                xml = ReadFile("Hubs.xml");
                List<Hub> hubs = new List<Hub>();
                hubs = ConvertFromXml<List<Hub>>(xml);

                foreach (Hub hub in hubs)
                {
                    set.hubs.Add(hub.Guid, hub);
                }
            }

            if (File.Exists("Bulletins.xml"))
            {
                xml = ReadFile("Bulletins.xml");
                List<Bulletin> bulletins = new List<Bulletin>();
                bulletins = ConvertFromXml<List<Bulletin>>(xml);

                foreach (Bulletin bulletin in bulletins)
                {
                    set.bulletins.Add(bulletin.Guid, bulletin);
                }
            }

            if (File.Exists("Admins.xml"))
            {
                xml = ReadFile("Admins.xml");
                List<Admin> admins = new List<Admin>();
                admins = ConvertFromXml<List<Admin>>(xml);

                foreach (Admin admin in admins)
                {
                    set.admins.Add(admin.Guid, admin);
                }
            }

            if (File.Exists("Editors.xml"))
            {
                xml = ReadFile("Editors.xml");
                List<Editor> editors = new List<Editor>();
                editors = ConvertFromXml<List<Editor>>(xml);

                foreach (Editor editor in editors)
                {
                    set.editors.Add(editor.Guid, editor);
                }
            }

            //NameValueCollection appSettings = ConfigurationManager.AppSettings;

            //set.botSettings.autoConnect = Convert.ToBoolean((string)appSettings["Autoconnect"]);
            //set.botSettings.botDescription = (string)appSettings["Botdescription"];
            //set.botSettings.botEmail = (string)appSettings["Botemail"];
            //set.botSettings.botName = (string)appSettings["Botname"];
            //set.botSettings.confirmExit = Convert.ToBoolean((string)appSettings["Confirmexit"]);
            //set.botSettings.minimizeToTray = Convert.ToBoolean((string)appSettings["Minimizetray"]);

            return set;
        }

        public void SaveSettings(Settings settings)
        {
            string xml = "";

            if (settings.hubs.Count > 0)
            {
                List<Hub> hubs = new List<Hub>(settings.hubs.Values);
                xml = ConvertToXml(hubs);
                WriteFile(xml, "Hubs.xml");
            }

            if (settings.bulletins.Count > 0)
            {
                List<Bulletin> bulletins = new List<Bulletin>(settings.bulletins.Values);
                xml = ConvertToXml(bulletins);
                WriteFile(xml, "Bulletins.xml");
            }

            if (settings.admins.Count > 0)
            {
                List<Admin> admins = new List<Admin>(settings.admins.Values);
                xml = ConvertToXml(admins);
                WriteFile(xml, "Admins.xml");
            }

            if (settings.editors.Count > 0)
            {
                List<Editor> editors = new List<Editor>(settings.editors.Values);
                xml = ConvertToXml(editors);
                WriteFile(xml, "Editors.xml");
            }

            //Chimera2 set = Chimera2.Default;

            //set["AutoConnect"] = settings.botSettings.autoConnect;
            //set["Description"] = settings.botSettings.botDescription;
            //set["Email"] = settings.botSettings.botEmail;
            //set["Name"] = settings.botSettings.botName;
            //set["ConfirmExit"] = settings.botSettings.confirmExit;
            //set["MinimizeToTray"] = settings.botSettings.minimizeToTray;

            //set.Save();
        }

        bool WriteFile(string text, string filename)
        {
            try
            {
                using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write))
                {
                    StreamWriter sw = new StreamWriter(fs);
                    sw.Write(text);
                    sw.Flush();
                    sw.Close();
                }
            }
            catch
            {               
            }
            return false;
        }

        string ReadFile(string filename)
        {
            if (File.Exists(filename))
            {
                try
                {
                    using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
                    {
                        StreamReader sr = new StreamReader(fs);
                        string text = sr.ReadToEnd();
                        sr.Close();

                        return text;
                    }
                }
                catch { }
            }
            return null;
        }

        static string ConvertToXml(object item)
        {
            XmlSerializer xmlser = new XmlSerializer(item.GetType());
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
            {
                xmlser.Serialize(ms, item);
                UTF8Encoding textconverter = new UTF8Encoding();
                return textconverter.GetString(ms.ToArray());
            }
        }

        static T ConvertFromXml<T>(string xml)
        {
            XmlSerializer xmlser = new XmlSerializer(typeof(T));
            using (System.IO.StringReader sr = new System.IO.StringReader(xml))
            {
                return (T)xmlser.Deserialize(sr);
            }
        }

    }
}
